package com.example.sunlight.interneteverything;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.sunlight.interneteverything.View.HealthPlayView;
import com.example.sunlight.interneteverything.View.HealthRunView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by SunLight on 2017/8/8.
 */

public class HealthManagement extends Activity {
    RelativeLayout run, play;
    LineChartView lineChart;
    TextView hear, steps, blak;

    String[] date = {"0:00", "3:00", "6:00", "9:00", "12:00", "15:00", "18:00", "21:00", "24:00"};//X轴的标注
    int[] score = {500, 420, 900, 330, 300, 740, 270, 400, 250, 452};//图表的数据点
    private List<PointValue> mPointValues;
    private List<AxisValue> mAxisXValues;
    List<Integer> num = new ArrayList<Integer>();
    int i = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
//                    getAxisXLables();
                    getAxisPoints();
                    init();
                    break;
                case 0x456:
                    steps.setText("步数" + new Random().nextInt(100) % (100 - 50 + 1) + 50);
                    blak.setText("血压" + (new Random().nextInt(9) + 5) + "0");
                    break;
                case 0x789:
                    hear.setText(new Random().nextInt(80) % (80 - 60 + 1) + 60 + "");
                    break;
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 2000);
        }
    };
    TextUpdate textUpdate;
    TextHeal textHeal;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health);
        FindView();
        HealthRunView runView = new HealthRunView(this);
        HealthPlayView playView = new HealthPlayView(this);
        run.addView(runView);
//        play.addView(playView);

        getAxisXLables();
        getAxisPoints();
        init();
        handler.postDelayed(runnable, 2000);
        textUpdate = new TextUpdate();
        textUpdate.start();
        textHeal = new TextHeal();
        textHeal.start();
    }

    public void FindView() {
        run = (RelativeLayout) findViewById(R.id.run);
        play = (RelativeLayout) findViewById(R.id.play);
        lineChart = (LineChartView) findViewById(R.id.lin_char);
        hear = (TextView) findViewById(R.id.hear);
        steps = (TextView) findViewById(R.id.steps);
        blak = (TextView) findViewById(R.id.blak);
    }


    /**
     * 设置X 轴的显示
     */
    private void getAxisXLables() {
        mAxisXValues = new ArrayList<AxisValue>();
        for (int i = 0; i < date.length; i++) {
            mAxisXValues.add(new AxisValue(i).setLabel(date[i]));
        }
    }

    /**
     * 图表的每个点的显示
     */
    private void getAxisPoints() {
        num.clear();
        mPointValues = new ArrayList<PointValue>();
//        for (int i = 0; i < score.length; i++) {
//            mPointValues.add(new PointValue(i, score[i]));
//        }
        for (int j = 0; j < 10; j++) {
            num.add(new Random().nextInt(1000));
        }

        for (int i = 0; i < num.size(); i++) {
            mPointValues.add(new PointValue(i, num.get(i)));
        }
    }

    public void init() {
        Line line = new Line(mPointValues).setColor(Color.parseColor("#00c0ff"));  //折线的颜色（橙色）
        List<Line> lines = new ArrayList<Line>();
        line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状  这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
        line.setCubic(true);//曲线是否平滑，即是曲线还是折线
        line.setFilled(true);//是否填充曲线的面积
        line.setHasLabels(true);//曲线的数据坐标是否加上备注
//        line.setHasLabelsOnlyForSelected(true);//点击数据坐标提示数据（设置了这个line.setHasLabels(true);就无效）
        line.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示
        line.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示（每个数据点都是个大的圆点）
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);

        //坐标轴
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(false);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        axisX.setTextColor(Color.WHITE);  //设置字体颜色
        //axisX.setName("date");  //表格名称
        axisX.setTextSize(10);//设置字体大小
        axisX.setMaxLabelChars(8); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        axisX.setValues(mAxisXValues);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX); //x 轴在底部
        //data.setAxisXTop(axisX);  //x 轴在顶部
        axisX.setHasLines(false); //x 轴分割线

        // Y轴是根据数据的大小自动设置Y轴上限(在下面我会给出固定Y轴数据个数的解决方案)
        Axis axisY = new Axis();  //Y轴
        axisY.setName("");//y轴标注
        axisY.setTextSize(10);//设置字体大小
        data.setAxisYLeft(axisY);  //Y轴设置在左边
        //data.setAxisYRight(axisY);  //y轴设置在右边


        //设置行为属性，支持缩放、滑动以及平移
        lineChart.setInteractive(true);
        lineChart.setZoomType(ZoomType.HORIZONTAL);
        lineChart.setMaxZoom((float) 2);//最大方法比例
        lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        lineChart.setLineChartData(data);
        lineChart.setVisibility(View.VISIBLE);
        /**注：下面的7，10只是代表一个数字去类比而已
         * 当时是为了解决X轴固定数据个数。见（http://forum.xda-developers.com/tools/programming/library-hellocharts-charting-library-t2904456/page2）;
         */
        Viewport v = new Viewport(lineChart.getMaximumViewport());
        v.left = 0;
        v.right = 7;
        lineChart.setCurrentViewport(v);
    }

    class TextHeal extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    handler.sendEmptyMessage(0x789);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class TextUpdate extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10000);
                    handler.sendEmptyMessage(0x456);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
